﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
namespace ConfExpert.Forms.Depo
{
    public partial class Lokasyon : XForm
    {
        public Lokasyon()
        {
            InitializeComponent();
        }

        int selIndex
        {
            get
            {
                int ret = -1;
                switch (cmbTip.EditValue.ToStr())
                {
                    case "Shelf": ret = 0; break;
                    case "Allocated": ret = 1; break;
                    case "Owner": ret = 2; break;
                    case "Product": ret = 3; break;
                }
                return ret;
            }
        }

        private void Lokasyon_Load(object sender, EventArgs e)
        {
            gv1.Init("GetLokasyon", selIndex, intParse(gle.EditValue), chkM.Checked);
            gv1.AddSummary("Qty");
            AddGroupSummary(gv1, "TotalRecords", "Qty");
            gv1.OptionsBehavior.Editable = false;

            GridColumn col = gv1.Columns["Qty"];
            col.ColumnEdit = new RepositoryItemProgressBar();
            ((RepositoryItemProgressBar)col.ColumnEdit).ShowTitle = true;
            ((RepositoryItemProgressBar)col.ColumnEdit).PercentView = false;
            ((RepositoryItemProgressBar)col.ColumnEdit).Maximum = 120;

            //Qty'nin yanına yeni bir sütun ekle ve onu ProgressBar olarak göster!
            //((RepositoryItemProgressBar)col.ColumnEdit).ReadOnly = false;

            gv1.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(gv1_SelectionChanged);

            cmbTip.EditValueChanged += cmbTip_EditValueChanged;
            gle.Visibility = chkM.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            gle.EditValueChanged += new EventHandler(gle_EditValueChanged);
            //gle.Top = 12;
            chkM.CheckedChanged += chkM_CheckedChanged;

            //cmbTip.Properties.ReadOnly = false;

            teArtikel.EditValueChanged += TeArtikel_EditValueChanged;
            teArtikel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            gv1.BestFitColumns();
        }


        //       void teArtikel_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        private void TeArtikel_EditValueChanged(object sender, EventArgs e)
        {
            string NewValue = teArtikel.EditValue.ToStr();
            if (NewValue.ToString() != string.Empty)
                gv1.ActiveFilterString = "ProductId like '%" + NewValue.ToString() + "%'";
            else
                gv1.ActiveFilterString = string.Empty;
        }

        void chkM_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selIndex >= 1 || selIndex <= 2)
            {
                if (chkM.Checked)
                {
                    int marka = intParse(gle.EditValue);
                    if (marka > 0)
                        gle.EditValue = AppSession.Lookups.Brand.FindByBrandId(marka).CustomerId;
                    gle.Edit.Assign(RI(Rp.V_Customer));
                }
                else
                {
                    int musteri = intParse(gle.EditValue);
                    if (musteri > 0)
                        gle.EditValue = intParse(AppSession.Lookups.Brand.Select("CustomerId = " + musteri.ToString())[0]["BrandId"]);
                    gle.Edit.Assign(RI(Rp.Brand));
                }
            }

            if (intParse(gle.EditValue) > 0)
                RefreshData();
        }

        bool suspended = false;
        void gle_EditValueChanged(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            if (!suspended)
            {
                gv1.Columns["PRO"].UnGroup();
                gv1.MakeColumnVisible("OwnerId", "ShelfId", "ProductId", "Scan");
                //Shelf
                //Allocated
                //Owner
                //Product
                switch (selIndex)
                {
                    case 0:
                        if (gle.EditValue != null)
                            gv1.MakeColumnInvisible("ShelfId");
                        break;

                    case 2:
                        if (gle.EditValue != null)
                            gv1.MakeColumnInvisible("OwnerId");
                        break;

                    case 3:
                        if (gle.EditValue != null)
                            gv1.MakeColumnInvisible("ProductId", "Scan");
                        break;
                }

                gv1.RefreshDataSource(true, "GetLokasyon", selIndex, intParse(gle.EditValue), chkM.Checked);

                if (selIndex <= 3)
                {
                    gv1.Columns["PRO"].Group();
                    gv1.ExpandAllGroups();
                }
                //else                
                //    gv1.Columns["ProductId"].UnGroup();

                gv1.BestFitColumns();
            }
        }

        void cmbTip_EditValueChanged(object sender, EventArgs e)
        {
            chkM.Visibility = (selIndex == 1 || selIndex == 2) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;

            suspended = true;
            gle.EditValue = null;
            //gle.Top = 12;
            gle.Visibility = (selIndex > -1) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
            suspended = false;

            switch (selIndex)
            {
                case 0:
                    teArtikel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    gle.Edit.Assign(RI(Rp.Shelf));
                    break;
                case 1:
                    teArtikel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    if (chkM.Checked)
                        gle.Edit.Assign(RI(Rp.V_Customer));
                    else
                        gle.Edit.Assign(RI(Rp.Brand));
                    break;
                case 2:
                    teArtikel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    teArtikel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    gle.Edit.Assign(RI(Rp.V_Customer));
                    break;
                case 3:
                    gv1.ActiveFilterString = string.Empty;
                    teArtikel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    gle.Edit.Assign(RI(Rp.V_Kumas));
                    break;
            }
        }

        private void gv1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            GetQty(gv1, "Qty", labelControl1);
        }


        private void GetQty(GridView gv, string FieldName, LabelControl lbl)
        {
            double toplamuzunluk = 0.0f;
            int adet = 0;
            try
            {
                foreach (int rowHandle in gv.GetSelectedRows())
                    if (rowHandle > -1)
                        if (gv.GetRowCellValue(rowHandle, "Qty").ToString() != string.Empty)
                        {
                            toplamuzunluk += double.Parse(gv.GetRowCellValue(rowHandle, "Qty").ToString());
                            adet++;
                        }

            }
            finally { lbl.Text = String.Format("Adet: {0:#,##0} | Metre: {1:#,##0.##} m.", adet, toplamuzunluk); }
        }

    }
}